# -*- coding: utf-8 -*-
# created on 2016/11/30
from mathsolver.functions.shulie.Fundamental import sl_shulie_name
from mathsolver.functions.shulie.ShuLie_getVariable import ShuLieGetVariable
from mathsolver.functions.base import *
from mathsolver.functions.shulie.ShuLie_getSeqPoly import ShuLieGetSeqPoly


class ShuLieDengChaProve001(BaseFunction):
    """
    在数列{a_{n}}中,a_{1}=1,a_{n+1}=2a_{n}+2^{n}.设b_{n}=\\frac{{a}_{n}}{{2}^{n-1}}.证明:数列{b_{n}}是等差数列.
    """
    def solver(self, *args):
        seq = args[0].sympify()
        stepsolver = ShuLieGetSeqPoly(self.known).solver(args[0])
        self.steps += stepsolver.steps
        self.label.update(stepsolver.label)
        seq_values = stepsolver.output[0].sympify()
        for seq_value in seq_values:
            fn = seq_value
            fn1 = fn.subs({sympify('n'): sympify('n+1')})
            d = (fn1 - fn).expand().simplify()
            if sympify('n') not in d.free_symbols:
                self.steps.append(["", "∴%s是等差数列" % (new_latex(seq))])
        self.label.add("定义法证明等差数列")
        return self


class ShuLieDengChaProve(BaseFunction):
    CLS = [ShuLieDengChaProve001]

    def solver(self, *args):
        known = self.known
        r = None
        for cl in ShuLieDengChaProve.CLS:
            try:
                r = cl(known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
